package com.itany.service.impl;



import com.itany.entity.*;
import com.itany.mapper.ManagerUserMapper;
import com.itany.mapper.PermissionMapper;
import com.itany.mapper.RolePermissionMapper;
import com.itany.mapper.UserRoleMapper;
import com.itany.service.ManagerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class ManagerUserServiceImpl implements ManagerUserService {
    @Autowired
    private ManagerUserMapper mapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public ManagerUser findByUsernameAndPassword(String username, String password) {
        ManagerUserExample example = new ManagerUserExample();
        example.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<ManagerUser> list = mapper.selectByExample(example);
        if(list.isEmpty()){
            return null;
        }else{
            return list.get(0);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void modifyPassword(Integer id, String password) {
        ManagerUser managerUser = new ManagerUser();
        managerUser.setId(id);
        managerUser.setPassword(password);
        mapper.updateByPrimaryKeySelective(managerUser);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByParams(String name) {
        List<ManagerUser> list = mapper.selectByParams(name);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()){
            for (ManagerUser managerUser : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",managerUser.getId());
                u.put("username",managerUser.getUsername());
                u.put("password",managerUser.getPassword());
                List<Role> roles = mapper.selectByUserId(managerUser.getId());
                u.put("roles",roles);
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void insertManagerUser(String password, String name, Integer id, String ids) {
        ManagerUser managerUser = new ManagerUser();
        managerUser.setUsername(name);
        managerUser.setPassword(password);
        if(id != 0){
            managerUser.setCommpanyid(id);
        }
        mapper.insertSelective(managerUser);
        String[] list = ids.split(",");
        for(String i : list){
            UserRole userRole = new UserRole();
            userRole.setUserid(managerUser.getId());
            userRole.setRoleid(Integer.parseInt(i));
            userRoleMapper.insertSelective(userRole);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public ManagerUser queryById(Integer id) {
        ManagerUser managerUser = mapper.selectByPrimaryKey(id);
        List<Role> roles = mapper.selectByUserId(id);
        managerUser.setRoles(roles);
        return managerUser;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void modifyManagerUser(String password, String name, Integer companyid, String ids, Integer id) {
        ManagerUser managerUser = new ManagerUser();
        managerUser.setId(id);
        managerUser.setUsername(name);
        managerUser.setPassword(password);
        if(companyid != 0){
            managerUser.setCommpanyid(companyid);
        }
        mapper.updateByPrimaryKey(managerUser);
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.or().andUseridEqualTo(id);
        userRoleMapper.deleteByExample(userRoleExample);
        String[] list = ids.split(",");
        for(String i : list){
            UserRole userRole = new UserRole();
            userRole.setUserid(id);
            userRole.setRoleid(Integer.parseInt(i));
            userRoleMapper.insertSelective(userRole);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String,Object> queryRoles(Integer id) {
        Map<String,Object> map = new HashMap<>();
        List<Role> roles = mapper.selectByUserId(id);
        map.put("roles",roles);
        return map;
    }

    @Override
    public List<Map<String, Object>> queryList(Integer id) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Set<Integer> id_set = new HashSet<>();
        UserRoleExample example = new UserRoleExample();
        example.or().andUseridEqualTo(id);
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        if(!userRoles.isEmpty()){
            for(UserRole userRole : userRoles){
                RolePermissionExample rolePermissionExample = new RolePermissionExample();
                rolePermissionExample.or().andRoleidEqualTo(userRole.getRoleid());
                List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(rolePermissionExample);
                if(!rolePermissions.isEmpty()){
                    for(RolePermission rolePermission : rolePermissions){
                        id_set.add(rolePermission.getPermissionid());
                    }
                }else{
                    return null;
                }
            }
            List<Permission> permissions = new ArrayList<>();
            for(Integer i : id_set){
                Permission permission = permissionMapper.selectByPrimaryKey(i);
                if(permission.getParentid() == null){
                    permissions.add(permission);
                }
            }
            for(Permission permission:permissions){
                Map<String, Object> u = new HashMap<>();
                u.put("menuid",permission.getId());
                u.put("icon","icon-sys");
                u.put("menuname",permission.getName());
                List<Map<String, Object>> mapList1 = new ArrayList<>();
                List<Permission> permissionList = new ArrayList<>();
                PermissionExample permissionExample = new PermissionExample();
                permissionExample.or().andParentidEqualTo(permission.getId());
                List<Permission> permissions1 = permissionMapper.selectByExample(permissionExample);
                for(Permission permission1 : permissions1){
                    if(id_set.contains(permission1.getId())){
                        permissionList.add(permission1);
                    }
                }
                if(!permissionList.isEmpty()){
                    for(Permission permission1 : permissionList){
                        Map<String, Object> a = new HashMap<>();
                        a.put("menuid",permission1.getId());
                        a.put("menuname",permission1.getName());
                        a.put("icon","icon-nav");
                        a.put("url",permission1.getUrl());
                        mapList1.add(a);
                    }
                    u.put("menus",mapList1);
                }else{
                    u.put("menus",null);
                }
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }
}
